package com.companyName.sample.common.bean;

import java.io.Serializable;

import com.companyName.sample.common.constant.CommConstants.RequestParamNames;
import com.companyName.sample.common.constant.ResponseMsg;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseData<T> implements Serializable {

	/**
	 * serialVersionUID:TODO
	 */
	private static final long serialVersionUID = 5936034458844414582L;

	@JsonProperty(RequestParamNames.Code)
	int code;

	@JsonProperty(RequestParamNames.Result)
	T result;

	@JsonProperty(RequestParamNames.Message)
	String message;

	public ResponseData(int code, T result) {
		this.code = code;
		this.result = result;
	}

	public ResponseData(int code, T result, String message) {
		this.code = code;
		this.result = result;
		this.message = message;
	}
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public Object getResult() {
		return result;
	}
	
	public void setResult(T result) {
		this.result = result;
	}

	public boolean success() {
		return this.code == ResponseMsg.SUCCESS.getCode();
	}

	@Override
	public String toString() {
		try {
			return new ObjectMapper().writeValueAsString(this);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
